package com.ldzl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ldzl.dto.CapacityDTO;
import com.ldzl.pojo.CkItemRecpt;
import com.ldzl.pojo.CkStorageArea;
import com.ldzl.pojo.CkStorageLocation;
import com.ldzl.pojo.CkWarehouse;
import com.ldzl.service.CkStorageAreaService;
import com.ldzl.service.CkStorageLocationService;
import com.ldzl.mapper.CkStorageLocationMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* @author 风止
* @description 针对表【ck_storage_location(库区表)】的数据库操作Service实现
* @createDate 2025-07-10 17:18:31
*/
@Service
public class CkStorageLocationServiceImpl extends ServiceImpl<CkStorageLocationMapper, CkStorageLocation>
    implements CkStorageLocationService{

    @Autowired
    private CkStorageAreaService sas; //库位服务

    /**
     * 查询所有库区
     * @param location
     * @return
     */
    @Override
    public List<CkStorageLocation> findLocation(CkStorageLocation location) {
        //查询所有库区信息
        List<CkStorageLocation> listLocation = super.baseMapper.selectLocation(location);
        //查询库区容量信息
        List<CapacityDTO> capList = super.baseMapper.findCapacity_l(location);
        for (CkStorageLocation loc : listLocation) {
            for (CapacityDTO cap : capList) {
                if (loc.getLocation_id().equals(cap.getLocation_id())) {
                    // 检查 max_weight 是否为零或 null
                    if (cap.getMax_weight() != null && cap.getMax_weight().compareTo(BigDecimal.ZERO) != 0) {
                        // 计算已用容量百分比
                        BigDecimal percentage = cap.getUsed_weight()
                                .divide(cap.getMax_weight(), 4, BigDecimal.ROUND_HALF_UP)
                                .multiply(new BigDecimal("100"));

                        // 设置保留两位小数
                        percentage = percentage.setScale(2, BigDecimal.ROUND_HALF_UP);
                        loc.setUsed_capacity(percentage);
                    }else {
                        // 如果 max_weight 为零或 null，可以设置默认值或进行其他处理
                        System.out.println("最大容量为零或 null，无法计算百分比。");
                        loc.setUsed_capacity(BigDecimal.ZERO); // 设置默认值为0
                    }
                }
            }
        }
        return listLocation;
    }

    /**
     * 新增/修改 库区
     * @param location
     * @return
     */
    @Override
    public int saveLocation(CkStorageLocation location) {
        //判断名称是否重复
        CkStorageLocation loc = super.getOne(new QueryWrapper<CkStorageLocation>()
                .eq("location_name",location.getLocation_name())
                .eq("warehouse_id",location.getWarehouse_id())
                .eq("is_delete",0)
        );

        if(location.getLocation_id() == null){
            //新增数据
            //赋值创建时间和更新时间
            location.setCreate_time(new Date());
            location.setUpdate_time(new Date());

            if(loc != null)
                return 3; //名称重复
        }else{
            //修改数据
            location.setUpdate_time(new Date());
            //清除创建人
            location.setCreate_by(null);

            CkStorageLocation loc2 = super.getOne(new QueryWrapper<CkStorageLocation>()
                    .eq("location_id",location.getLocation_id())
                    .eq("is_delete",0)
            );

            if(loc != null && !loc.getLocation_name().equals(loc2.getLocation_name()))
                return 3; //名称重复
        }
        if(super.saveOrUpdate(location))
            return 1;
        return 2;
    }

    /**
     * 删除仓库下的库区
     * @param warehouse_id
     * @return
     */
    @Override
    public boolean delete_warehouse_id(Long warehouse_id) {
        return super.baseMapper.delete_warehouse_id(warehouse_id) > 0;
    }

    /**
     * 删除库区
     * @param location_id
     * @return
     */
    @Override
    public boolean deleteLocation(Long location_id) {
        CkStorageLocation location = new CkStorageLocation();
        location.setLocation_id(location_id);
        location.setIs_delete("1");
        //删除库区下的库位
        if (!sas.delete_location_id(location_id))
            System.out.println("移除库位失败："+location_id);

        return super.saveOrUpdate(location);
    }

    /**
     * 批量删除库区
     * @param ids
     * @return
     */
    @Override
    public String deleteLocation_batch(List<Long> ids) {
        String isSuccess = "库区id为：";

        List<CkStorageLocation> listLocation = new ArrayList<>();
        for (Long location_id : ids) {
            CkStorageLocation location = new CkStorageLocation();
            location.setLocation_id(location_id);
            location.setIs_delete("1");

            //查询库区下的库位信息
            List<CkStorageArea> listArea = sas.list(new QueryWrapper<CkStorageArea>()
                    .eq("location_id",location_id)
                    .eq("is_delete",0)
            );
            if(listArea != null && listArea.size() > 0){
                //删除库区下的库位
                if (!sas.delete_location_id(location_id))
                    isSuccess = isSuccess + location_id + ",";
                else
                    listLocation.add(location);
            }else
                listLocation.add(location);

        }
        isSuccess = isSuccess + "移除对应库位失败";
        if (!super.saveOrUpdateBatch(listLocation))
            return "fail";
        return isSuccess;
    }
}




